import axios from "axios";



const baseURL = '/tushare';
const request = axios.create({ baseURL })

const token = "f7fac7d93d700e5b3f2c586c958414763a742c0f74a60176749d0718"

// 请求拦截器
request.interceptors.request.use(function (config) {
    // 将token添加到请求体中
    if (config.method === 'post' || config.method === 'put' || config.method === 'patch') {
        if (config.data) {
            config.data.token = token;
        } else {
            config.data = { token: token };
        }
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 响应拦截器
request.interceptors.response.use(function (response) {
    if (response.data.code === 0) {
        return response.data.data
    } else {
        return response.data;
    }
},
    function (error) {
        return Promise.reject(error);
    }
);

// 获取K线图数据,type = 1/2/3，分别对应日线、周线、月线
export const getKlineDataApi = (type, ts_code) => {
    let apiName = ""
    let month = 2
    if (type === 1) {
        apiName = "daily"
    }
    else if (type === 2) {
        apiName = "weekly"
        month = 12
    }
    else if (type === 3) {
        apiName = "monthly"
        month = 48
    }
    let { startDate, endDate } = getDateRange(month);
    return request.post("", {
        api_name: apiName,
        params: {
            ts_code: ts_code,
            start_date: startDate,
            end_date: endDate,
        }
    })
}

function getDateRange(months) {
    // 获取当前日期
    let endDate = new Date();
    // 获取 months 月前的日期
    let startDate = new Date(endDate);
    startDate.setMonth(endDate.getMonth() - months);

    // 将日期格式化为 YYYYMMDD
    function formatDate(date) {
        let year = date.getFullYear();
        let month = String(date.getMonth() + 1).padStart(2, '0');
        let day = String(date.getDate()).padStart(2, '0');
        return `${year}${month}${day}`;
    }

    let startDateStr = formatDate(startDate);
    let endDateStr = formatDate(endDate);

    return { startDate: startDateStr, endDate: endDateStr };
}

// 获取股票实时数据
export const getStockRealDataApi = (ts_code) => {
    let part = ts_code.split(".")[0];
    // const tokenList = ["E6E67BFA-D85E-4714-8E72-A90C0393A251"]
    const tokenList = ["E6E67BFA-D85E-4714-8E72-A90C0393A251","CF4C852E-D324-4A56-9AF2-4E68409F671E"
        // ,"4B62252A-A42A-4D16-9285-75A7F84ECE79","FAD8F56F-A371-4C82-8CCF-9414D942AC60"
    ]

    return new Promise((resolve, reject) => {
        const tryNextToken = (index) => {
            if (index >= tokenList.length) {
                // 所有 token 都尝试过了，仍未找到响应码为 200 的请求
                reject(new Error('所有token均已使用，未得到响应码为 200 的结果'));
                return;
            }
            const token = tokenList[index];
            const url = `https://api.mairui.club/hsrl/ssjy/${part}/${token}`;
            // console.log(url);
            // axios.get(url)
            //     .then(response => {
            //         if (response.status === 200) {
            //             // 响应码为 200，返回该响应
            //             resolve(response);
            //         } else {
            //             // 响应码不为 200，尝试下一个 token
            //             tryNextToken(index + 1);
            //         }
            //     })
            //     .catch(error => {
            //         // 请求出错，尝试下一个 token
            //         tryNextToken(index + 1);
            //     });
        };
        // 从第一个 token 开始尝试
        tryNextToken(0);
    })
}   